草庐IT

Java final 与 C++ const

全部标签

c++ - MSVC 使用 constexpr 从可变参数模板方法中的基本模板参数中吞下 const if

我有一个问题,我几乎可以肯定这是一个MSVC错误,但也许我遗漏了什么。这是实际代码的简化版本:templateclassInnerType{};templateclassOuterType{public:voidfoo1(){ifconstexpr(true){bar(InnerType());}}voidfoo2(){if(true){bar(InnerType());}}voidbar(InnerType){}};如您所见,foo1()和foo2()之间的唯一区别是constexprif。以下是我尝试在MSVC中编译一些测试时发生的情况:OuterTypetest1;test1.f

c++ - 为什么 lambda 没有从到达范围捕获类型 const double,而 const int 是?

我似乎无法理解为什么以下类型为constint的代码可以编译:intmain(){usingT=int;constTx=1;autolam=[](Tp){returnx+p;};}$clang++-clambda1.cpp-std=c++11$而这个类型为constdouble的不是:intmain(){usingT=double;constTx=1.0;autolam=[](Tp){returnx+p;};}$clang++-clambda2.cpp-std=c++11lambda1.cpp:5:32:error:variable'x'cannotbeimplicitlycaptur

c++ - static_assert 无法将 const char* 模板参数识别为 constexpr : g++ bug?

考虑以下定义。charright_string[]="::right_one.";charwrong_string[]="::wrong_one.";templatevoidf(){static_assert(str==::right_string,"Passme::right_string!");}structTest{staticconstexprcharright_string[]="template_struct::right_one";staticconstexprcharwrong_string[]="template_struct::wrong_one";template

c++ - 使用移动赋值从算术运算符重载返回 const 值

假设我有以下最小示例类:#includeclassFoo{public:Foo()=default;Foo(constFoo&)=default;Foo(Foo&&)noexcept=default;Foo&operator=(constFoo&rhs){std::cout这会按预期打印move。现在根据EffectiveC++Item3,我应该从operator+返回constFoo以避免像a+b=c这样的构造,即://Toavoida+b=cconstFoooperator+(constFoo&rhs)const{}不幸的是,这突然开始调用复制赋值而不是移动赋值运算符。[我在Ubu

c++ - 如何以 clang 格式强制使用 east const?

是否有clang格式的标志将“constwest”更改为“eastconst”,以便以下内容:voidfun(conststd::string&s);将被重新格式化为:voidfun(std::stringconst&s); 最佳答案 从Clang-Format14开始,有QualifierAlignment。将以下行添加到您的.clang-format配置文件中:QualifierAlignment:Right 关于c++-如何以clang格式强制使用eastconst?,我们在Sta

c++ - 为什么 const temporary 选择调用非 const 成员函数而不是 const 成员函数?

这个问题在这里已经有了答案:Consttemporaryfromtemplatetypeandwhyusestd::add_const?(2个答案)关闭9年前。示例代码取自:http://en.cppreference.com/w/cpp/types/add_cv(我稍微修改了一下。)structfoo{voidm(){std::coutvoidcall_m(){T().m();}intmain(){call_m();call_m();//here}输出是:Non-cvNon-cv在第二次调用中,T是const限定的,所以T()应该调用const版本,对吗?还是我错过了一些特殊规则?

c++ - 强制转换为模板中的引用似乎抛弃了 const-ness

考虑以下C++代码:typedefstd::string&mutable_string_ref;conststd::stringstr="abc";mutable_string_refref(str);这显然会导致编译器错误,因为您无法创建对常量字符串的可变引用。对于GCC4.7.2,产生的错误是:error:invalidinitializationofreferenceoftype‘mutable_string_ref{akastd::basic_string&}’fromexpressionoftype‘conststring{akaconststd::basic_string}

c++ - 模板代码中的转发引用与 const 左值引用

我最近一直在研究C++中的转发引用,下面是我目前对该概念的理解的快速总结。假设我有一个模板函数foo对T类型的单个参数进行转发引用.templatevoidfoo(T&&arg);如果我用左值调用这个函数,那么T将被推断为T&制作arg参数类型为T&由于引用折叠规则T&&&->T&.如果使用未命名的临时函数调用此函数,例如函数调用的结果,则T将被推断为T制作arg参数类型为T&&.内部foo然而,arg是一个命名参数,所以我需要使用std::forward如果我想将参数传递给其他函数并仍然保持其值类别。templatevoidfoo(T&&arg){bar(std::forward(a

c++ - 重载 const 和非 const 转换运算符返回数组类型时出现 MSVC 错误 C2593

最近,我尝试使用转换运算符来替代operator[]。喜欢下面的代码:#includeclassfoo{public:usingtype=int[1];public:operatortype&(){returndata;}operatortypeconst&()const{returndata;}private:typedata;};intmain(){foof;f[0]=1;//errorhappensherestd::cout我发现它适用于G++但不适用于MSVCv141(2017)。MSVC报告:errorC2593:'operator['isambiguousnote:coul

c++ - const_iterator<T> 和 iterator<const T> 有什么区别?

假设我正在实现一个集合,比如std::vector.我需要实现iterator和const_iterator,但一旦我做了iterator可以const_iterator不只是实现为iterator(其中T是集合中包含的类型)?肯定有一些原因导致这行不通,因为关于如何在实现iterator时重用代码存在一百万个问题。和const_iterator但他们都没有说“只需使用constT作为类型”。 最佳答案 std::iterator_traits::value_type应该是T对于const_iterator,但是constT1表示i